<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author jay
 */
class Categories extends CI_Controller{
 	
	function index() { 
		
	}
	
 	function getCategories() { 
 		$this->load->model('categories_model');
		
		// Get categories from Model's method
		$data = $this->categories_model->getCategories();
		
		$item = array();
		$list = array();
		
		foreach ($data as $row) {
			array_push($item, array("text"=>$row->category, "id"=>$row->id, "desc"=>$row->description));
		}
		$list = json_encode($item);	
		//$list = '[{"text": "2. Wrapper plugins"},{"text": "3. Summary"},{"text": "4. Questions and answers"}]';
		log_message('info','$list = '. $list);
		
		// Return the json data
		header('Content-Type: application/json; charset=utf-8');
		echo(json_encode($item));
 	}

 	function deleteCategory() {
 		$id = $this->input->post('id');
 		
        $this->load->model('categories_model');
        $this->categories_model->deleteCategory($id);
        
		header('Content-Type: application/json; charset=utf-8');
		echo (json_encode($id));
    }
	
	function saveCategory() {
		$catname = $this->input->post('catname');
		$catdesc = $this->input->post('catdesc');
		$userid = $this->input->post('userid');
		$catid = $this->input->post('catid');
		
		$data = array(
			'catname'=>$catname, 
			'catdesc'=>$catdesc,
			'userid'=>$userid,
			'catid'=>$catid
		);
		
		$this->load->model('categories_model');
        $this->categories_model->saveCategory($data);
		
		header('Content-Type: application/json; charset=utf-8');
		echo (json_encode(array('data'=>'success')));
	}
	
}
?>